package com.xing.controller;

import com.alibaba.fastjson.JSON;
import com.xing.entity.pojo.SystemFunction;
import com.xing.entity.pojo.SystemRole;
import com.xing.entity.pojo.SystemUser;
import com.xing.service.RoleFunctionService;
import com.xing.service.SystemFunctionService;
import com.xing.service.SystemRoleService;
import com.xing.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemFunctionService systemFunctionService;

    @Autowired
    private RoleFunctionService roleFunctionService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<SystemRole> roles = systemRoleService.findAll();
        model.addAttribute("roles",roles);
        return "userRole/userRole";
    }

    @RequestMapping("/findByUser")
    @ResponseBody
    public String findByUser(String roleId){
        List<SystemUser> users = systemUserService.findByRoleId(roleId);
        return JSON.toJSONString(users);
    }

    @RequestMapping("/findRoleAll")
    @ResponseBody
    public String findRoleAll(){
        List<SystemRole> roles = systemRoleService.findAll();
        return JSON.toJSONString(roles);
    }
    @RequestMapping("/findByRole/{roleId}")
    public String findByUser(@PathVariable("roleId") String roleId, Model model){
        if(roleId.equals("add")){
            return "userRole/addRole";
        }else{
            SystemRole role = systemRoleService.selectByPrimaryKey(roleId);
            model.addAttribute("role",role);
            return "userRole/updateRole";
        }
    }
    @PostMapping ("/addRole")
    @ResponseBody
    public String addUser(SystemRole systemRole,@RequestParam(value = "functionId",required = false) String[] functionId){
        systemRole.setRoleId(UUID.randomUUID().toString());
        int i = systemRoleService.insertSelective(systemRole);
        if(functionId.length>0){
            roleFunctionService.insertSelective(functionId,systemRole.getRoleId());
        }
       if(i>0){
           return "添加成功";
       }else {
           return "添加失败";
       }
    }

    @PostMapping("/updateRole")
    public String updateUser(SystemRole systemRole){
        systemRoleService.updateByPrimaryKey(systemRole);
        return"redirect:/role/findAll";
    }
    @RequestMapping("/deleteRole/{roleId}")
    public String deleteUser(@PathVariable("roleId") String roleId)
    {
        roleFunctionService.deleteByRoleId(roleId);
        systemRoleService.deleteByPrimaryKey(roleId);
        return"redirect:/role/findAll";
    }

    @RequestMapping("/deleteSelectedRole")
    @ResponseBody
    public String deleteSelectedRole(String[] ids){
        if(systemRoleService.deleteSelectedRole(ids)){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @RequestMapping("/findFunAll")
    @ResponseBody
    public String sunFun(){
        return JSON.toJSONString(systemFunctionService.findFunAll());
    }

    @RequestMapping("/findRoleFunId")
    @ResponseBody
    public String updateFun(String roleId){
        return JSON.toJSONString(roleFunctionService.findByUId(roleId));
    }

    @RequestMapping("/updateFun")
    @ResponseBody
    public String findRoleFunId(@RequestParam("functionId") String[] functionId,@RequestParam("roleId") String roleId){
        roleFunctionService.deleteByRoleId(roleId);
        if(functionId.length>0){
            roleFunctionService.insertSelective(functionId,roleId);
        }else {
            return "方法不能为空";
        }
       return "修改成功";
    }
}
